
module.exports = {
    /**
     *  @title 选择图片
     */
    async ChooseImage(e) {
        const {key}=uni.$get_data(e)
        const imgList=this.formData[key]||[]
        const img_arr=await new Promise((success, fail) => {
            uni.chooseImage({
                count: 4, //默认9
                sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album'], //从相册选择
                success: (res) => {
                   success(res) 
                }
            });
        });
        for (let i = 0; i < img_arr.tempFilePaths.length; i++) {
            const element = img_arr.tempFilePaths[i];
            let img = await this.UpdataImg(element)
            console.log(img);
            if (imgList.length != 0) {
                this.formData[key] = [...imgList,img]
                this["show_" + key] = this["show_" +key].concat(element)
            } else {
                this.formData[key] = [img]
                this["show_" +key] = [element]
            }
        }
    },
    /**
    *  @title 图片放大
    */
    ViewImage(e) {
        const {key}=uni.$get_data(e)
        uni.previewImage({
            urls: this[key],
            current: e.currentTarget.dataset.url
        });
    },
    /**
    *  @title 删除图片
    */
    DelImg(e) {
        const {key}=uni.$get_data(e)
        if (key) {
           uni.showModal({
            title: '提示',
            content: '您确认要删除这张图片吗？',
            cancelText: '再看看',
            confirmText: '再见',
            success: res => {
                if (res.confirm) {
                    this.formData[key].splice(e.currentTarget.dataset.index, 1)
                    this["show_"+key].splice(e.currentTarget.dataset.index, 1)
                }
            }
        }) 
        }else{
            uni.$err("请传入data-key,key是删除的字段名")
        }
        
    },
    UpdataImg(file){
       return new Promise((success,fail)=>{
           uni.uploadFile({
               url: uni.$up_file, //仅为示例，非真实的接口地址
               filePath:file,
               name: 'file',
               formData: {
                   'user': 'test'
               },
               success: (res) => {
                    let json= JSON.parse(res.data)
                    success(json.data.url)
               },
               fail:(err)=>{
                   console.log(err);
               }
           });
       })
    },
    /**
     * @title 单选方法
     * @param {*} e 
     */
    PickerChange(e) {
        // let key=e.currentTarget.dataset.key
        const {
            currentTarget: {
                dataset: {
                    key, data
                }
            }
        } = e
        let arr
        try {
            arr = data.split(",")
            
        } catch (error) {
            arr = data
        }
        this.formData = {
           ...this.formData,
           [key]:arr[e.detail.value]
       }
    },
}